package apiexample.transform.statistics;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import apiexample.transform.CentralDataPool;

import com.apiexample.server.dao.BaseDao;
import com.apiexample.server.dao.ClassDao;
import com.apiexample.server.entity.ClassEntity;
import com.apiexample.server.entity.ExampleEntity;

import dao.APIDao;
import dao.PageDao;
import dao.QueryPageDao;
import dao.SegmentDao;
import dao.entity.PageEntity;
import dao.entity.QueryEntity;
import dao.entity.QueryPageRelation;
import dao.entity.SegmentEntity;

public class StatisticsOnExampleDB {

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {
		statistic_2();
	}

	/**
	 * 进行一些统计工作：统计网站的分布情况 输出：site page_numbers
	 * 
	 * @throws FileNotFoundException
	 */
	public static void statistic_2() throws FileNotFoundException {
		PrintStream fos_out = new PrintStream(new File(
				"M:/apiexample/statistic_sites_number.txt"));
		System.setOut(fos_out);
		System.out.println("统计网站的分布情况 输出：site page_numbers");
		System.out.println("Sites\tNumber_of_Pages");

		Connection example_conn = dao.BaseDao.getTestConn();

		ArrayList<String> allLinks = PageDao.getLinkList(example_conn,
				PageEntity.ALL);

		HashMap<String, Integer> map = new HashMap<String, Integer>();
		for (String link : allLinks) {
			String host = null;
			try {
				host = new URL(link).getHost();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (host == null) {
				continue;
			}
			Integer exampleCount = map.get(host);
			if (exampleCount != null) {
				map.remove(host);
				map.put(host, exampleCount + 1);
			} else {
				map.put(host, 1);
			}
		}
		Iterator<String> hosts = map.keySet().iterator();
		
		while (hosts.hasNext()) {
			String host = hosts.next();
			Integer count = map.get(host);
			System.out.println(host + "\t" + count);
			System.err.println(host + "\t" + count);
		}
		fos_out.close();
	}

	/**
	 * 进行一些统计工作：统计每个API关联的相关网页的数目；统计每个网页包含的内容片段的数目
	 * 
	 * @throws FileNotFoundException
	 */
	public static void statistic_1() throws FileNotFoundException {

		int projectId = 11;
		PrintStream fos_out = new PrintStream(new File(
				"M:/apiexample/statistic_project_" + projectId + ".txt"));
		System.setOut(fos_out);

		Connection server_conn = BaseDao.getTestConn();
		Connection example_conn = dao.BaseDao.getTestConn();

		CentralDataPool.persist(example_conn, server_conn);

		HashMap<Integer, Integer> apiIdMapQueryID = new HashMap<Integer, Integer>();
		for (QueryEntity query : CentralDataPool.allQueries) {
			apiIdMapQueryID.put(new Integer(query.getApi_id()), new Integer(
					query.getId()));
		}
		// exampleDB
		HashMap<String, Integer> apiNameMapId = APIDao
				.getNameMapID(example_conn);

		HashMap<Integer, ClassEntity> classIdEntityMap = new HashMap<Integer, ClassEntity>();
		List<ClassEntity> classes = ClassDao.getAllClassList(server_conn);

		HashMap<String, Integer> apiNameMapPages = new HashMap<String, Integer>();
		HashSet<Integer> pageIdSet = new HashSet<Integer>();

		System.out.println("Statistic on project:" + projectId);
		System.out.println("=============================================");
		for (ClassEntity classEntity : classes) {
			// classIdEntityMap.put(new Integer(classEntity.getId()),
			// classEntity);
			if (classEntity.getProject_id() == projectId) {
				String name = classEntity.getName();
				Integer apiIdExample = apiNameMapId.get(name);
				if (apiIdExample == null) {
					continue;
				}
				Integer queryId = apiIdMapQueryID.get(apiIdExample);

				if (queryId != null) {
					ArrayList<QueryPageRelation> queryPages = QueryPageDao
							.getPageListByQueryID(example_conn, queryId
									.intValue());
					if (queryPages != null) {
						apiNameMapPages.put(name, queryPages.size());
					} else {
						apiNameMapPages.put(name, 0);
					}

					for (QueryPageRelation queryPageRelation : queryPages) {
						pageIdSet.add(new Integer(queryPageRelation
								.getPage_id()));
					}
				}
			}
		}
		HashMap<Integer, Integer> pageIdMapSegments = new HashMap<Integer, Integer>();
		for (Integer page_id : pageIdSet) {
			ArrayList<SegmentEntity> segments = SegmentDao
					.getSegmentListByPageID(example_conn, page_id);
			if (segments != null) {
				pageIdMapSegments.put(page_id, segments.size());
			}
		}

		Iterator<String> apiNamesIte = apiNameMapPages.keySet().iterator();
		System.out.println("APINAME\tPAGES");
		while (apiNamesIte.hasNext()) {
			String apiName = apiNamesIte.next();
			System.out.println(apiName + "\t"
					+ apiNameMapPages.get(apiName).intValue());
		}
		System.out
				.println("----------------------------------------------------------");
		System.out.println("PAGEID\tSEGMENTS");
		Iterator<Integer> pageIdsIte = pageIdMapSegments.keySet().iterator();
		while (pageIdsIte.hasNext()) {
			Integer pageId = pageIdsIte.next();
			System.out.println(pageId + "\t" + pageIdMapSegments.get(pageId));
		}

		try {
			server_conn.close();
			example_conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
